<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'分红订单列表'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['ordersn']){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('order',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['fanfee']=pdo_getcolumn('jy_cun9fan_fanlog',array('orderid'=>$item['ordersn'],'uniacid'=>$_SESSION['uniacid']),'fanfee');
			$item['detail']='/commission/list/ordersn='.$item['ordersn'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'<red>[温馨提示]：只有存在上级，或者支持内购（属于分红商），才会产生佣金！</br>非必要，不建议删除记录，怕数据异常！</red>','edit'=>false,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'price','title'=>'订单金额','type'=>'string'),
			array('name'=>'commission_price','title'=>'收益基数','type'=>'string'),
			array('name'=>'fanfee','title'=>'预计返额','type'=>'string'),
			array('name'=>'commission_money','title'=>'销售收益','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','name'=>'detail','title'=>'明细'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'name'=>'ordersn','title'=>'订单号'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if($id){
		$orderid=pdo_getcolumn('jy_cun9fan_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'ordersn');
		$mid=pdo_getcolumn('jy_cun9fan_fanlog',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']),'mid');
		$lastfanfee=pdo_getcolumn('jy_cun9fan_fanlog',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']),'lastfanfee');
		if($lastfanfee>0){
			//cfc('core')->member_credit($mid,'credit2',$lastfanfee,1,'jy_cun9fan','删除订单返回余额');
		}else{
			exi('不能删除！订单返额已完成','error');
		}
		pdo_delete('jy_cun9fan_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		//pdo_delete('jy_cun9fan_fanlog',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$orderid=pdo_getcolumn('jy_cun9fan_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'ordersn');
				$mid=pdo_getcolumn('jy_cun9fan_fanlog',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']),'mid');
				$lastfanfee=pdo_getcolumn('jy_cun9fan_fanlog',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']),'lastfanfee');
				if($lastfanfee>0){
					//cfc('core')->member_credit($mid,'credit2',$lastfanfee,1,'jy_cun9fan','删除订单返回余额');
				}else{
					exi('不能删除！订单返额已完成','error');
				}
				pdo_delete('jy_cun9fan_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
				//pdo_delete('jy_cun9fan_fanlog',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
?>